﻿using Exiled.API.Features;
using Exiled.API.Features.Pools;
using Exiled.Events.EventArgs.Player;
using Exiled.Events.EventArgs.Server;
using PlayerRoles;
using System;
using System.Collections.Generic;

namespace AbilityBrawl
{
    [GameMode("技能大乱斗", "每个人都拥有技能，杀死敌方玩家抢夺技能，活下去！", RoleReservationEnabled = false)]
    public class AbilityBrawl : GameMode
    {
        public static List<Ability> AvailableAbilities = new List<Ability>();
        public static bool Initialized = false;

        public override IEnumerable<RoleTypeId> GetScpDrawPool(int scp_count)
        {
            return new RoleTypeId[]
            {
                 RoleTypeId.Scp096, RoleTypeId.Scp3114, RoleTypeId.Scp173, RoleTypeId.Scp049, RoleTypeId.Scp106, RoleTypeId.Scp939
            };
        }

        public override string GetSpawnQueue()
        {
            return "401344632043416204431";
        }

        protected override void EndGame()
        {
            RgCore.RgCore.RoundStarted -= RgCore_RoundStarted;
            Exiled.Events.Handlers.Player.Died.Unsubscribe(Player_Died);
            Exiled.Events.Handlers.Server.RespawningTeam.Unsubscribe(Server_Respawning);
        }

        protected override void InitializeGame()
        {
            AvailableAbilities.Clear();
            string[] Banned_Abilities = new string[]
            {
                "Scp204_1", "Scp204_2", "Scp204_Attr_1", "Scp378_1", "Scp378_Attr_1", "Scp999_2", "Scp999_Attr_1", "Scp1793_Attr_1"
            };
            AvailableAbilities.AddRange(AbilityManager.RegisteredAbilities.Where(x => !Banned_Abilities.Contains(x.AbilityId)));
            RgCore.RgCore.RoundStarted += RgCore_RoundStarted;
            Exiled.Events.Handlers.Player.Died.Subscribe(Player_Died);
            Exiled.Events.Handlers.Server.RespawningTeam.Subscribe(Server_Respawning);
        }

        private static IEnumerator<float> Player_Died(DiedEventArgs ev)
        {
            List<AbilityInfo> abilites =
                AbilityManager.Abilities.TryGetValue(ev.Player, out var list) ? ListPool<AbilityInfo>.Pool.Get(list) : ListPool<AbilityInfo>.Pool.Get();
            ev.Player.RevokeAbility();
            if (!AbilityManager.Abilities.TryGetValue(ev.Attacker, out var own))
            {
                own = new List<AbilityInfo>();
            }
            abilites.RemoveAll(x => own.Contains(x));
            if (own.Count(x => x.Ability is OffensiveAbility) >= 3)
            {
                abilites.RemoveAll(x => x.Ability is OffensiveAbility);
            }
            if (abilites.Count > 0)
            {
                var ability = abilites[UnityEngine.Random.Range(0, abilites.Count)];
                GiveAbility(ev.Attacker, ability.Ability);
            }

            yield break;
        }

        private static IEnumerator<float> Server_Respawning(RespawningTeamEventArgs ev)
        {
            ev.Players.ForEach(x => GiveAbility(x));
            yield break;
        }

        private static void RgCore_RoundStarted()
        {
            foreach (Player player in Player.List)
            {
                GiveAbility(player);
            }
        }

        public static void GiveAbility(Player player, Ability ability = null)
        {
            if (ability == null)
            {
                ability = AvailableAbilities[UnityEngine.Random.Range(0, AvailableAbilities.Count)];
            }
            player.GrantAbility(ability);
            player.SendRawMsg("ab", $"你获得了[{ability.Text}]技能 [<color=#1E90FF>{ability.Cooldown}秒</color>]：\n{ability.Description}", 10, ScreenLocation.CenterBottom, false);
        }

        protected override void OnLoaded()
        {
            typeof(Abilities).GetNestedTypes().ForEach(x => AbilityManager.RegisterAbility((Ability)Activator.CreateInstance(x)));
        }
    }
}